/**
 * AdministradorConexion.java
 */
/**
 *  AdSec: Administrador de Secundarias
 *  Copyright (C) 2009 Daniela Ramírez Chaves (dannyrch@gmail.com)
 *  Copyright (C) 2009 Leonel Salazar Valverde (leo87sv@gmail.com)
 *
 *  This file is part of AdSec.
 *  AdSec is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AdSec is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AdSec.  If not, see <http://www.gnu.org/licenses/>.
 */

package adsec.datos;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class AdministradorConexion {
    private String puerto = "3306";
    private String servidor = "localhost";
	private String nombreBD = "liceoescazu";
    private String urlBD = "jdbc:mysql://" + getServidor() + ":" + getPuerto() + "/liceoescazu";
    private String usuario = "root";
    private String password = "password";
    private Connection conexion;
    private static AdministradorConexion adminConexion;
    public final static int QUERY_OK = 0;
    public final static int QUERY_ERROR = 1;
    public final static int QUERY_HALF_OK = 2;

    private AdministradorConexion(){
        try{
            //registrar Driver de MySQL
            Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException claseNoEncontrada){
            System.err.println("Driver de MySQL no encontrado");
            claseNoEncontrada.printStackTrace();
        }
    }//fin de constructor

    public static AdministradorConexion getInstancia(){
        if(adminConexion == null){
            adminConexion = new AdministradorConexion();
        }
        return adminConexion;
    }//fin de getInstancia

    public Connection getConexion(){
        try{
            if(this.conexion == null){
                //Conseguir conexión
                this.conexion = DriverManager.getConnection(this.getUrlBD(), this.getUsuario(), this.getPassword());
            }
        }catch(SQLException sqlException){
            System.err.println("No se logró abrir la conexión a la Base de Datos");
            sqlException.printStackTrace();
        }
        return this.conexion;
    }//fin de getConexion

    public void cerrarConexion(){
        try{
            if(this.conexion != null && !this.conexion.isClosed()){
                this.conexion.close();
                this.conexion = null;
            }
        }catch(SQLException sqlException){
            System.err.println("No se logró cerrar la conexión a la Base de Datos");
            sqlException.printStackTrace();
        }
    }//fin de cerrarConexion

    /**
     * @return the urlBD
     */
    public String getUrlBD() {
        return urlBD;
    }

    /**
     * @param urlBD the urlBD to set
     */
    public void setUrlBD(String servidor, String puerto, String nombreBD) {
        this.setServidor(servidor);
        this.setPuerto(puerto);
		this.setNombreBD(nombreBD);
        this.urlBD =  "jdbc:mysql://" + servidor + ":" + puerto + "/" + nombreBD;
    }

    /**
     * @return the usuario
     */
    public String getUsuario() {
        return usuario;
    }

    /**
     * @param usuario the usuario to set
     */
    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the puerto
     */
    public String getPuerto() {
        return puerto;
    }

    /**
     * @param puerto the puerto to set
     */
    public void setPuerto(String puerto) {
        this.puerto = puerto;
    }

    /**
     * @return the servidor
     */
    public String getServidor() {
        return servidor;
    }

    /**
     * @param servidor the servidor to set
     */
    public void setServidor(String servidor) {
        this.servidor = servidor;
    }

	/**
	 * @return the nombreBD
	 */
	public String getNombreBD() {
		return nombreBD;
	}

	/**
	 * @param nombreBD the nombreBD to set
	 */
	public void setNombreBD(String nombreBD) {
		this.nombreBD = nombreBD;
	}

}//fin de clase AdministradorConexion
